﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Options;
using Microsoft.IdentityModel.Tokens;
using ProjectTemplate.Common.Response;
using ProjectTemplate.Identity.Authorization;
using ProjectTemplate.Identity.Data;
using ProjectTemplate.Identity.Models;
using ProjectTemplate.IServices;
using ProjectTemplate.Model.ViewModels.Account;
using ProjectTemplate.Services;
using System.IdentityModel.Tokens.Jwt;
using System.Security.Claims;
using System.Text;
using Ubiety.Dns.Core;

namespace ProjectTemplate.Controllers
{
    /// <summary>
    /// 用户控制器
    /// </summary>
    [Route("api/[controller]")]
    [ApiController]
    public class AccountController : ControllerBase
    {
        private readonly UserManager<User> _userManager;
        private readonly ILogger<AccountController> _logger;
        private readonly IAccountService _accountService;
        private readonly JwtOptions _options;

        public AccountController(
            UserManager<User> userManager,
            ILogger<AccountController> logger,
            IOptions<JwtOptions> options,
            IAccountService accountService
      )
        {
            _userManager = userManager;
            _logger = logger;
            _accountService = accountService;
            _options = options.Value;
        }

        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="login"></param>
        /// <returns></returns>
        [HttpPost("Login")]
        public async Task<Response<LoginViewModel>> Login(LoginDTO login)
        {
            var result = await _accountService.Login(login);

            return result;
        }

        /// <summary>
        /// 注册
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost("Register")]
        public async Task<Response<bool>> Register(RegisterDTO dto)
        {
            var result = await _accountService.Register(dto);

            return result;
        }
    }
}
